package com.logistics.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("imageManagerAction")
@Scope("prototype")
public class ImageManagerAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5139230838004418402L;

	private File upfile;
	
	/**
	 * 上传文件
	 *
	 * @return
	 */
	public File getUpfile() {
		return upfile;
	}

	/**
	 * 上传文件
	 *
	 * @param upfile
	 */
	public void setUpfile(File upfile) {
		this.upfile = upfile;
	}

	private String upfileFileName;
	
	/**
	 * 上传文件名
	 *
	 * @return
	 */
	public String getUpfileFileName() {
		return upfileFileName;
	}

	/**
	 * 上传文件名
	 *
	 * @param upfileFileName
	 */
	public void setUpfileFileName(String upfileFileName) {
		this.upfileFileName = upfileFileName;
	}

	private String upfileContentType;

	/**
	 * upfileContentType
	 *
	 * @return
	 */
	public String getUpfileContentType() {
		return upfileContentType;
	}

	/**
	 * upfileContentType
	 *
	 * @param upfileContentType
	 */
	public void setUpfileContentType(String upfileContentType) {
		this.upfileContentType = upfileContentType;
	}

	
	private String param1;
	
	/**
	 * param1
	 *
	 * @return
	 */
	public String getParam1() {
		return param1;
	}

	/**
	 * param1
	 *
	 * @param param1
	 */
	public void setParam1(String param1) {
		this.param1 = param1;
	}

	private String param2;
	/**
	 * param2
	 *
	 * @return
	 */
	public String getParam2() {
		return param2;
	}

	/**
	 * param2
	 *
	 * @param param2
	 */
	public void setParam2(String param2) {
		this.param2 = param2;
	}

	private String pictitle;
	
	/**
	 * pictitle
	 *
	 * @return
	 */
	public String getPictitle() {
		return pictitle;
	}

	/**
	 * pictitle
	 *
	 * @param pictitle
	 */
	public void setPictitle(String pictitle) {
		this.pictitle = pictitle;
	}
	
	/**
	 * 在线管理
	 */
	public void imageManager() {
		String imgStr ="";
		List<File> files = getFiles(getUeditorImageUploadFolder(), new ArrayList<File>());
		for(File file :files ){
			imgStr+= file.getPath().replace(getUeditorImageUploadFolder(), "") + "ue_separate_ue";
		}
		if(StringUtils.isNotEmpty(imgStr)){
	        imgStr = imgStr.substring(0,imgStr.lastIndexOf("ue_separate_ue")).replace(File.separator, "/").trim();
	    }
		outputString(imgStr);
	}
	
	
	public List<File> getFiles(String realpath, List<File> files) {
		
		File realFile = new File(realpath);
		if (realFile.isDirectory()) {
			File[] subfiles = realFile.listFiles();
			for(File file :subfiles ){
				if(file.isDirectory()){
					getFiles(file.getAbsolutePath(),files);
				}else{
					if(!getFileType(file.getName()).equals("")) {
						files.add(file);
					}
				}
			}
		}
		return files;
	}

	public String getFileType(String fileName){
		String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
		Iterator<String> type = Arrays.asList(fileType).iterator();
		while(type.hasNext()){
			String t = type.next();
			if(fileName.endsWith(t)){
				return t;
			}
		}
		return "";
	}
}
